//
//  BankFormViewController.m
//  MoneyBook
//
//  Created by MyoungHo Kim on 11. 7. 8..
//  Copyright 2011 Daum. All rights reserved.
//

#import "BankFormViewController.h"

@implementation BankFormViewController
@synthesize bankTitle = _bankTitle;
@synthesize typeSegment = _typeSegment;
@synthesize bankType = _bankType;
@synthesize bankId = _bankId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                  target:self
                                                                                  action:@selector(saveAction)];
        [self.navigationItem setRightBarButtonItem:rightBtn];
        [rightBtn release];
    }
    return self;
}

- (void)dealloc{
    [_bankTitle release];
    [_typeSegment release];
    [_bankId release];
    [_bankType release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
    _bankTitle.borderStyle = UITextBorderStyleRoundedRect;
    _bankTitle.textColor = [UIColor blackColor];
    _bankTitle.font = [UIFont systemFontOfSize:15.0];
    _bankTitle.backgroundColor = [UIColor whiteColor];
    _bankTitle.autocorrectionType = UITextAutocorrectionTypeNo;
    _bankTitle.keyboardType = UIKeyboardTypeDefault;
    _bankTitle.returnKeyType = UIReturnKeyDone;
    _bankTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
    _bankTitle.placeholder = @"이름을 입력 해 주세요.";
    
    
    [self.typeSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [self.typeSegment addTarget:self action:@selector(typeSegmentAction:) forControlEvents:UIControlEventValueChanged];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [self setBankTitle:nil];
    [self setTypeSegment:nil];
    [self setBankId:nil];
    [self setBankType:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark UISegmentedDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)typeSegmentAction:(id)sender{
    NSLog(@"typeSegmentAction:%d", [_typeSegment selectedSegmentIndex]);
    self.bankType = [NSNumber numberWithInt:[_typeSegment selectedSegmentIndex]];    
}
#pragma mark -
#pragma mark UIBarButtonBankDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveAction{
    if(_isCreateMode){
        [[SQLHandler sharedInstance] createBankWithTitle:[_bankTitle text]
                                                    type:_bankType];
    }else{
//        NSLog(@"saveActoin: %d, %@, %d", [_bankId intValue], [_bankTitle text], [_bankType intValue]);
        [[SQLHandler sharedInstance] updateBankId:_bankId 
                                            title:[_bankTitle text]
                                             type:_bankType];
    }
    [[self navigationController] popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadBank:(NSNumber*)bankId{
    _isCreateMode = NO;
    SQLHandler*sql = [SQLHandler sharedInstance];
    NSDictionary *dic = [sql findBankById:bankId];
    //    NSLog(@"dic:%@",dic);
    self.bankId = bankId;
    [self.bankTitle setText:[dic objectForKey:@"title"]];
    
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)createMode{
    _isCreateMode = YES;    
}
@end
